<?php

class User_model extends CI_Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();		
		$this->load->module_model('admin', 'category_model', 'category');
		$this->_table = $this->config->item('database_tables');
	}
	
	
	// Public methods
	public function user_exchange($id, $active)
	{
		$data = array
				(
					'active' => $active
				);
			//var_dump($data);die();
		$this->db->where('user_id', $id);
		$this->db->update($this->_table['lr_users'], $data);
	}
	
	public function get_all_user($limit, $start)
	{
		$this->db->select('*');	
		$this->db->from('user');
		$this->db->limit($limit, $start);
		$this->db->order_by('id', 'desc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function count_all_user()
	{
		$this->db->select('id');
		$this->db->from('user');
		return $this->db->count_all_results();
	}
	
	public function get_user($id)
	{
		$this->db->select('*');
		$this->db->where('user_id', $id);
		$query = $this->db->get($this->_table['lr_users']);	
		if ($query->num_rows() == 1)
		{
			return $query->row_array();
		}
	}
	
	public function change_active_user($id = null,$active = 0)
	{
		$data = array
				(
					'active'=> $active
				);
		$this->db->where('id',$id);
		$this->db->update('user',$data);		
	}
	
	public function add_newscategory($news_id, $category_id)
	{
		$data = array
					(
						'news_id' => $news_id,
						'category_id' => $category_id
					);
					
		$this->db->insert($this->_table['newscategory'], $data);
	}
	
	public function get_categories($lang_id=null)
	{
		$this->db->select('id, name');
		if($lang_id)
		{
			$this->db->where('lang_id',$lang_id);
		}
		$query = $this->db->get($this->_table['category']);
			
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row)
			{
				$data[$row['id']] = $row['name'];
			}

			return $data;
		}
	}
	
	
	public function create_users($lang_id=null,$photo=null)
	{
		$secret_key = do_hash(uniqid() + time(), 'md5');
		$data = array
				(
					'username'	=> $this->input->post('username'),
					'password'	=> do_hash($this->input->post('password'), 'md5'),
					'fullname'	=> $this->input->post('fullname'),
					'adress'	=> $this->input->post('adress'),
					'email'		=> $this->input->post('email'),
					'mobile' 	=> $this->input->post('mobile'),
					'tel'		=> $this->input->post('tel'),
					'yahoo' 	=> $this->input->post('yahoo'),
					'skype' 	=> $this->input->post('skype'),
					//'birthdate' => $create_date,
					'FK_group_id' 	=> $this->input->post('group'),
					'location_id' 	=> '1',
					//'type'=>2,
					'avatar'=>($photo) ? $photo['file_name']:'',
					//'alt'=>'',
					//'lang_id'=>($lang_id) ? $lang_id:2,
					//'order'=>$this->get_last_order() + 1,
					'active' => $this->input->post('visible'),
				);
		$this->db->insert($this->_table['user'], $data);
	}

	public function edit_banner($id,$photo=null)
	{
		$status = ($this->input->post('visible'))? 1:0;
		$data = array
				(
					'name' => $this->input->post('name'),
					'url' => $this->input->post('url'),
					'target' => $this->input->post('target'),					
					'image'=>($photo) ? $photo['file_name']:'',
					'alt'=>'',					
					'description' => $this->input->post('description'),
					'status' => $status
				);
			//var_dump($data);die();
		$this->db->where('id', $id);
		$this->db->update($this->_table['link'], $data);
	}
	public function edit_link($id)
	{
		$data = array
				(
					'name' => $this->input->post('name'),
					'url' => $this->input->post('url'),
					'target' => $this->input->post('target'),					
					'image'=>'',
					'alt'=>'',					
					'description' => $this->input->post('description'),
					'status' => $this->input->post('visible'),
				);
		$this->db->where('id', $id);
		$this->db->update($this->_table['link'], $data);
	}

	public function delete_user($id)
	{
		$this->db->where('id', $id);
			
		$this->db->delete('user');
		//$this->reorganize_navigation();
	}

	protected function get_last_order()
	{
		$this->db->select('MAX(link.order) AS pos');
		
		$query = $this->db->get($this->_table['link'], 1);
			
		if ($query->num_rows() == 1)
		{
			$result = $query->row_array();
			
			return $result['pos'];
		}
	}

	public function reorganize_navigation()
	{
		$this->db->select('id');
		
		$query = $this->db->get($this->_table['link']);
			
		if ($query->num_rows() > 0)
		{
			$result =  $query->result_array();
			
			$i = 0;

			foreach ($result as $row)
			{
				$this->db->set('order', ++$i);
				$this->db->where('id', $row['id']);
				$this->db->update($this->_table['link']);
			}
		}
	}
}

/* End of file links_model.php */
/* Location: ./application/modules/admin/models/links_model.php */